import 'package:flutter/material.dart';
import 'musicItem.dart';

class AllAlbumScreen extends StatefulWidget {
  const AllAlbumScreen({super.key});
  @override
  State<AllAlbumScreen> createState() => _AllAlbumScreen();
}

class _AllAlbumScreen extends State<AllAlbumScreen> {
  bool _isInitialized = false;

  @override
  void initState() {
    super.initState();
    _initializeData();
  }

  void _initializeData() {
    if (!_isInitialized) {
      _isInitialized = true;
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My Music',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: Scaffold(
          appBar: AppBar(
            title: const Text("我的歌单",
              style: TextStyle(
                fontSize: 30,
                fontWeight: FontWeight.bold,
              ),
            ),
            actions: [
              IconButton(
                icon: const Icon(Icons.add),
                onPressed: () {
                  print("add MusicList");
                },
              ),
              IconButton(
                icon: const Icon(Icons.delete),
                onPressed: () {
                  print("delete MusicList");
                },
              ),
            ],
          ),
          body: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 所有歌曲卡片 - 使用固定的高度
              GestureDetector(
                onTap: () {
                  print("点击 所有歌曲");
                },
                child: Container(
                  height: 100, // 固定高度
                  margin: EdgeInsets.symmetric(horizontal: 16, vertical: 2), // 统一外边距
                  child: Card(
                    elevation: 4,
                    child: Row(
                      children: [
                        Padding(
                          padding: EdgeInsets.all(16),
                          child: Icon(Icons.music_note, size: 40),
                        ),
                        Text(
                          "所有歌曲",
                          style: TextStyle(
                            fontSize: 24,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
              // 列表内容
              Expanded(
                child: ListView(
                  children: buildAlbumList(),
                ),
              ),
            ],
          )
      ),
    );
  }

  List<Widget> buildAlbumList() {
    List<Widget> albumWidget = [];
    AllAlbum allAlbum = AllAlbum.instance;
    allAlbum.albumList.forEach((album) {
      albumWidget.add(
          GestureDetector(
            onTap: () {
              print("点击歌单：${album.albumName}");
            },
            child: Container(
              height: 100, // 固定高度
              margin: EdgeInsets.symmetric(horizontal: 16, vertical: 2), // 统一外边距
              child: Card(
                elevation: 4,
                child: Row(
                  children: [
                    Container(
                      width: 80,
                      height: 80,
                      margin: EdgeInsets.all(8), // 添加内边距确保图片不超出边界
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(4), // 统一圆角
                        child: Image.asset(
                          album.albumCover,
                          fit: BoxFit.cover,
                        ),
                      ),
                    ),
                    SizedBox(width: 8), // 减小间距
                    Expanded(
                      child: Padding(
                        padding: EdgeInsets.symmetric(vertical: 8),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            Text(
                              album.albumName,
                              style: TextStyle(
                                fontSize: 20,
                                fontWeight: FontWeight.w500,
                              ),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                            Text(
                              album.musicList.isNotEmpty
                                  ? "${album.musicList.first.musicName} - ${album.musicList.first.musicAuthor}"
                                  : "无歌曲",
                              style: TextStyle(fontSize: 14),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                            Text(
                              "${album.musicList.length} 首",
                              style: TextStyle(fontSize: 14, color: Colors.grey),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          )
      );
    });
    return albumWidget;
  }
}